package org.ddd.book.infrastructure.security;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ddd.book.domain.security.repository.IUserRepository;
import org.ddd.book.domain.security.vo.UserVO;
import org.ddd.book.infrastructure.rpc.SoaApi;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
@Slf4j
public class UserRepositorySoaImpl implements IUserRepository {
    private final SoaApi soaApi;

    @Override
    public UserVO get(Long userId) {
        SoaApi.UserInfo user;
        try {
            user = soaApi.findUser(userId);
            if (null == user) {
                return null;
            }
        } catch (Exception e) {
            log.error("rpc error : {}", e.getMessage());
            user = SpringUtil.getBean(SoaApi.SoaApiFailBack.class).findUser(userId);
        }
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(user, userVO);
        return userVO;
    }

}
